package com.pdsu.alumni.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pdsu.alumni.common.annotation.NoAuthentication;
import com.pdsu.alumni.common.result.Result;
import com.pdsu.alumni.pojo.Files;
import com.pdsu.alumni.pojo.Mail;
import com.pdsu.alumni.service.ICoverService;
import com.pdsu.alumni.service.IFilesService;
import com.pdsu.alumni.service.IProduceService;
import com.pdsu.alumni.util.DateUtil;
import com.pdsu.alumni.util.TencentMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @program: alumni
 * @description:
 * @author: Acerola
 * @create: 2021-11-30 09:13
 **/
@Api(tags = "测试")
@RestController
@RequestMapping("test")
@CrossOrigin
public class Test {
    @Autowired
    private IProduceService produceService;
    @Autowired
    private TencentMessage tencentMessage;
    @Autowired
    private ICoverService coverService;
    @Autowired
    private IFilesService filesService;

    @RequestMapping("/login")
    //@NoAuthentication
    public Result<String> result() {
        String string = "你好";
        return new Result<String>().success().put("你好");
    }

    @ApiOperation(value = "邮件测试")
    @PostMapping("/send")
    @NoAuthentication
    public Result<?> send(Mail mail) {
        if (produceService.send(mail))
            return new Result<>().success().put("邮件发送成功,已进入队列");
        else
            return new Result<>().error("邮件发送失败");
    }

    @ApiOperation(value = "短信验证码测试")
    @PostMapping("/register")
    @ResponseBody
    @CrossOrigin
    @NoAuthentication
    public Result<?> register(String num) {
        String[] strings = {num};
        String code = tencentMessage.messageSend(strings);
        return new Result<>().success().put("验证码是" + code);
    }

    @ApiOperation(value = "异常捕获测试")
    @PostMapping("/exception")
    @ResponseBody
    @CrossOrigin
    @NoAuthentication
    public Result<?> exception() {
        int i = 1;
        int j = 0;
        return new Result<>().success(String.valueOf(i / j));
    }

    @ApiOperation(value = "map测试")
    @PostMapping("/map")
    @ResponseBody
    @CrossOrigin
    @NoAuthentication
    public Result<?> exception(@RequestBody Map map) {
        System.out.println("**************");
        System.out.println(map);
        String type = (String) map.get("type");
        String uid = (String) map.get("uid");
        String name = (String) map.get("name");
        String url = (String) map.get("src");
        Files files = new Files();
        files.setFileName(name);
        files.setFileUrl(url);
        files.setFileType(type);
        files.setFileUid(uid);
        // files.setMid(message.getId());
        files.setFileTime(DateUtil.formatMinute(new Date()));
        QueryWrapper<Files> filesQueryWrapper = new QueryWrapper<>();
        filesQueryWrapper.eq("file_uid", files.getFileUid());
        filesService.update(filesQueryWrapper);
        return new Result<>().success();
    }
}
